package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import javax.net.ssl.SSLSocketFactory;

public class SSLclient {

	public static void sslConnect(String request, String hostname) throws Exception {

		int port = 443;
		SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
		Socket socket = new Socket(hostname, port);

		Socket sslSocket = socketFactory.createSocket(socket, hostname, port, true);
		// Create streams to securely send and receive data to the server
		InputStream in = sslSocket.getInputStream();
		OutputStream out = sslSocket.getOutputStream();

		out.write(request.getBytes());
		out.flush();
		BufferedReader r = new BufferedReader(new InputStreamReader(in));
		String line;

		while ((line = r.readLine()) != null) {
			System.out.println(line);
		}

		in.close();
		out.close();

	}

	public static void playRequest() throws Exception {
		String line = "";
		BufferedReader reader = new BufferedReader(new FileReader("request.txt"));
		String request = "";
		while ((line = reader.readLine()) != null) {
			request += line + "\r\n";
		}
		request+="\r\n";
		System.out.println(request);
		sslConnect(request, "signin.ebay.com");
	}

	public static void main(String[] args) throws Exception {
		//sslConnect("GET / HTTP/1.0\r\n\r\n", "www.google.com");
		playRequest();
	}
}
